package cn.tqfeiyang.springplus.system.controller;

import cn.tqfeiyang.springplus.system.service.ConfigService;
import cn.tqfeiyang.springplus.system.request.ConfigDTO;
import cn.tqfeiyang.springplus.system.response.ConfigVO;
import cn.tqfeiyang.springplus.system.request.ConfigSTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 参数前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-08
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/system/config")
@Tag(name = "系统/参数")
public class ConfigController {

    /**
     * 参数服务
     */
    @Resource
    private ConfigService configService;

    /**
     * 查询参数
     *
     * @param configSTO 查询数据
     * @return 参数列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询参数")
    @PreAuthorize("hasAnyAuthority('system:config:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<ConfigVO>> selectConfigPage(ConfigSTO configSTO) {
        return ApiResult.success(configService.selectConfigPage(configSTO));
    }

    /**
     * 读取参数
     *
     * @param configId 参数id
     * @return 参数信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取参数")
    @PreAuthorize("hasAnyAuthority('system:config:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<ConfigVO> selectConfigInfo(@RequestParam @EntityId Long configId) {
        return ApiResult.success(configService.selectConfigInfo(configId));
    }

    /**
     * 新建参数
     *
     * @param configDTO 请求数据
     * @return 参数信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建参数")
    @PreAuthorize("hasAnyAuthority('system:config:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<ConfigVO> createConfig(@RequestBody @Validated(WhenCreate.class) ConfigDTO configDTO) {
        Long configId = configService.createConfig(configDTO);
        return ApiResult.success(configService.selectConfigInfo(configId));
    }

    /**
     * 修改参数
     *
     * @param configId 参数id
     * @param configDTO 请求数据
     * @return 参数信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改参数")
    @PreAuthorize("hasAnyAuthority('system:config:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<ConfigVO> updateConfig(@RequestParam @EntityId Long configId, @RequestBody @Validated(WhenUpdate.class) ConfigDTO configDTO) {
        configService.updateConfig(configId, configDTO);
        return ApiResult.success(configService.selectConfigInfo(configId));
    }

    /**
     * 删除参数
     *
     * @param configId 参数id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除参数")
    @PreAuthorize("hasAnyAuthority('system:config:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteConfig(@RequestParam @EntityId Long configId) {
        configService.deleteConfig(configId);
        return ApiResult.success();
    }
}
